package com.personalkeeper;

import java.io.File;
import java.util.Date;

import com.personalkeeper.PersonalKeeper.Utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import static com.personalkeeper.PersonalKeeper.ID;
import static com.personalkeeper.PersonalKeeper.LATITUDE;
import static com.personalkeeper.PersonalKeeper.LONGITUDE;
import static com.personalkeeper.PersonalKeeper.DATE;
import static com.personalkeeper.PersonalKeeper.IMAGE_PATH;
import static com.personalkeeper.PersonalKeeper.TITLE;
import static com.personalkeeper.PersonalKeeper.DESCRIPTION;


public class ShowNoteActivity extends Activity {

    private static final String TAG = ShowNoteActivity.class.toString();

    private TextView mTitleView;
    private TextView mDescriptionView;
    private TextView mDateView;
    private ImageView mImageView;

    private long mId;
    private Date mDate;
    private String mImagePath;
    private String mTitle;
    private String mDescription;
    private Bundle extras;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_note);

        extras = getIntent().getExtras();
        if (extras.containsKey(ID) &&
                extras.containsKey(LATITUDE) &&
                extras.containsKey(LONGITUDE) &&
                extras.containsKey(DATE) &&
                extras.containsKey(IMAGE_PATH) &&
                extras.containsKey(TITLE) &&
                extras.containsKey(DESCRIPTION)) {
            mDateView = (TextView) findViewById(R.id.show_note_date);
            mTitleView = (TextView) findViewById(R.id.show_note_title);
            mDescriptionView = (TextView) findViewById(R.id.show_note_description);
            mImageView = (ImageView) findViewById(R.id.show_note_picture);

            mId = extras.getLong(ID);
            mDate = new Date(extras.getLong(DATE));
            mImagePath = extras.getString(IMAGE_PATH);
            mTitle = extras.getString(TITLE);
            mDescription = extras.getString(DESCRIPTION);

            mDateView.setText(mDate.toString());
            mTitleView.setText(mTitle);
            mDescriptionView.setText(mDescription);
            Drawable image;
            if (mImagePath != null) {
                if ((image = Drawable.createFromPath(mImagePath)) != null)
                    mImageView.setImageDrawable(image);
                else
                    mImageView.setImageResource(R.drawable.photo_deleted);
            } else
                mImageView.setImageResource(R.drawable.no_photo);
        } else {
            extras = null;
            Log.d(TAG, "Wrong arguments");
            finish();
        }
    }


    public void showNoteButton_Click(View v) {
        Intent intent = new Intent(this, MapDisplay.class);
        intent.putExtras(extras);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(PersonalKeeper.ACTION_SHOW_NOTE);
        startActivity(intent);
    }

    public void deleteNoteButton_Click(View v) {
        ContentResolver cr = getContentResolver();
        Uri noteUri = Uri.withAppendedPath(HistoryProvider.Notes.CONTENT_URI, String.valueOf(mId));
        if (cr.delete(noteUri, null, null) > 0) {
            if (mImagePath != null) {
                File image = new File(mImagePath);
                File preview = Utils.noteImagePathToPreview(image);

                if (!image.delete())
                    Log.d(TAG, "no image: " + image.toString());
                if (!preview.delete())
                    Log.d(TAG, "no preview image: " + preview.toString());
            }
            finish();
        } else
            Log.d(TAG, String.format("Error, can't delete note with id: %d", mId));
    }
}
